const Koa = require("koa");
const static = require("koa-static");
const views = require("koa-views");
const bodyParser = require("koa-bodyparser");
const session = require("koa-session");
const app = new Koa();
const path = require("path");
const useRoutes = require("../router/index");
//设置ejs为静态模板
app.use(views(path.resolve(__dirname,"../views"),{
  extension:'ejs'
}));
//设置静态资源服务器
app.use(static(path.resolve(__dirname,"../public")));

//设置session
app.keys = ['some secret hurr']; // 签名 默认
app.use(session({
  key: 'koa:sess', //cookie key (default is koa:sess)
  maxAge: 2*60*60*1000, // cookie 的过期时间 maxAge in ms (default is 1 days)
  overwrite: true, //是否可以 overwrite (默认 default true)
  httpOnly: true, //cookie 是否只有服务器端可以访问 httpOnly or not (default true)
  signed: true, //签名默认 true
  rolling: false, //在每次请求时强行设置 cookie，这将重置 cookie 过期时间（默认：false）
  renew: false, //(boolean) renew session when session is nearly expired,
},app));

//处理post请求
app.use(bodyParser());
app.use(async (ctx,next)=>{
  ctx.body = ctx.request.body;
  await next();
});
//配置所有路由
useRoutes(app);
module.exports = app;